package com.qimo.controller;


import com.qimo.model.Shop;
import com.qimo.model.User;
import com.qimo.service.ItemService;
import com.qimo.service.ShoppingCartService;
import com.qimo.service.UserService;
import com.qimo.tool.ResultInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class ShoppingCartController {
    @Resource
    private UserService userService;
    @Resource
    private ShoppingCartService shoppingCartService;

    @ResponseBody
    @RequestMapping("/item/addShoppingCart")
    public ResultInfo addShoppingCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入ShoppingCartController");

        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        int userId=Integer.parseInt(request.getParameter("userId"));
        int itemId=Integer.parseInt(request.getParameter("itemId"));

        String userName=request.getParameter("userName");
        String passWord=request.getParameter("passWord");
        System.out.println(userId+itemId+userName+itemId);
        User temp=this.userService.selectUser(userName);
        if(temp.getPassWord().equals(passWord)&&temp.getUserId()==userId) {
            System.out.println("用户信息正确,添加购物车");
           if(this.shoppingCartService.addShoppingCart(userId,itemId)>0)
            {
                ResultInfo resultInfo=new ResultInfo();
                resultInfo.setCode(1);
                resultInfo.setCount(shoppingCartService.selectShoppingCart(userId));
                return resultInfo;
            }
           else{

               return ResultInfo.failure("插入失败,未知冲突，我觉得应该不会出现这条数据的");
            }
        }
        else {

            return ResultInfo.failure("用户信息不正确，无法添加购物车");
        }


    }
    @ResponseBody
    @RequestMapping("/shoppingCart/delShoppingCart")
    public ResultInfo delShoppingCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入delShoppingCart");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        int userId=Integer.parseInt(request.getParameter("userId"));
        int itemId=Integer.parseInt(request.getParameter("itemId"));
        String userName=request.getParameter("userName");
        String passWord=request.getParameter("passWord");
        System.out.println(userId+","+userName+","+itemId+","+passWord);
        User temp=this.userService.selectUser(userName);
        if(temp.getPassWord().equals(passWord)&&temp.getUserId()==userId){
            System.out.println("用户信息正确,加载购物车列表");
            ResultInfo resultInfo=new ResultInfo();
            if(true)
            {
                this.shoppingCartService.delShoppingCart(userId,itemId);
                resultInfo.setCode(1);
                System.out.println("123123123123");
                resultInfo.setMsg("删除购物车商品成功");
                resultInfo.setCount(this.shoppingCartService.selectShoppingCart(userId));
                resultInfo.setResult(this.shoppingCartService.ShoppingCartList(userId,1));
                return resultInfo;
            }
            else
                return ResultInfo.failure();
        }
        else {

            return ResultInfo.failure("用户信息不正确，无法查看购物车");
        }


    }
    @ResponseBody
    @RequestMapping("/shoppingCart/alterShoppingCartNumber")
    public ResultInfo addShoppingCartNumber(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入alterShoppingCartNumber");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        int userId=Integer.parseInt(request.getParameter("userId"));
        int itemId=Integer.parseInt(request.getParameter("itemId"));
        System.out.println("进入123");
        int Number=Integer.parseInt(request.getParameter("Number"));
        System.out.println("进入123");
        String userName=request.getParameter("userName");

        String passWord=request.getParameter("passWord");
        System.out.println(userId+","+userName+","+itemId+","+passWord+","+Number);
        User temp=this.userService.selectUser(userName);
        if(temp.getPassWord().equals(passWord)&&temp.getUserId()==userId){
            System.out.println("用户信息正确,加载购物车列表");
            ResultInfo resultInfo=new ResultInfo();
            if(this.shoppingCartService.alterShoppingCartNumber(userId,itemId,Number)>0)
            {
            resultInfo.setCode(1);
            resultInfo.setMsg("修改购物车数量成功");
            resultInfo.setCount(this.shoppingCartService.selectShoppingCart(userId));
            return resultInfo;
            }
            else
                return ResultInfo.failure();
        }
        else {

            return ResultInfo.failure("用户信息不正确，无法查看购物车");
        }


    }
    @ResponseBody
    @RequestMapping("/shoppingCart/shoppingCartList")
    public ResultInfo shoppingCartList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入ShoppingCartlist");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        int userId=Integer.parseInt(request.getParameter("userId"));
        int Page=Integer.parseInt(request.getParameter("Page"));
        String userName=request.getParameter("userName");
        String passWord=request.getParameter("passWord");
        System.out.println(userId+","+userName+","+Page+","+passWord);
        User temp=this.userService.selectUser(userName);
        if(temp.getPassWord().equals(passWord)&&temp.getUserId()==userId){
            System.out.println("用户信息正确,加载购物车列表");
            ResultInfo resultInfo=new ResultInfo();
            resultInfo.setCode(1);
            resultInfo.setCount(this.shoppingCartService.selectShoppingCart(userId));
            System.out.println(resultInfo.getCount());
            resultInfo.setResult(this.shoppingCartService.ShoppingCartList(userId,Page));

            return resultInfo;
        }
        else {

            return ResultInfo.failure("用户信息不正确，无法查看购物车");
        }


    }
}

